/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dttung
 */
public class CupScoreComparator implements Comparator<Object> {
    public final String systemdir = "C:\\Users\\dttung\\Documents\\NetBeansProjects\\2013-2014\\src\\java\\";
    //public final String systemdir = "/var/lib/openshift/5224c8d94382ec6f2b00034b/jbossews/yff/";
    //public final String systemdir = "/Users/dttung/NetBeansProjects/trunk/2013-2014/src/java/";
    HashMap<String, Integer> mapScoresToSort; // diem trong bang
    HashMap<String, Float> mapDiff; // hieu so trong bang
    int currweek; // current week
    String table;
    String selcup;
    Utils myUtil;
    public CupScoreComparator(HashMap<String, Integer> mapToSort, HashMap<String, Float> mapdiff, 
            int w, String tbl, String cup)
    {
        mapScoresToSort = mapToSort;
        mapDiff = mapdiff;
        currweek = w;
        table = tbl;
        selcup = cup;
        myUtil = new Utils();
        try {
            myUtil.readYffScores();
            myUtil.readTeamScores();
        } catch (Exception ex) {
            Logger.getLogger(CupScoreComparator.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    private int getWeekThatMeet(String team1, String team2) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        int iCup = Integer.valueOf(selcup);
        //int week = (iCup - 1) * 8 + 2 + round;
        int round = currweek - 2 - (iCup -1) * 8;
        int roundThatMeet = -1;
        for (int i = 1; i <= round; i++)
        {
            ArrayList<String> matches = getMatches(table, selcup, i);
            if ((team1+team2).equals(matches.get(0)) || (team2+team1).equals(matches.get(0))
                    || (team1+team2).equals(matches.get(1)) || (team2+team1).equals(matches.get(1)))
            {
                roundThatMeet = i; break;
            }
        }
        return (iCup - 1) * 8 + 2 + roundThatMeet;
        
    }
    private ArrayList<String> getMatches(String tbname, String cup, int round) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        File tablefile = new File(systemdir + "tab_" + tbname + "_" + cup + ".txt" );
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tablefile),"UTF-8"));
        String line = "";
        ArrayList<String> teams = new ArrayList<String>();
        ArrayList<String> matches = new ArrayList<String>();
        while ((line = reader.readLine()) != null)
        {
            teams.add(line);
        }
        switch (round)
        {
            case 1:
                matches.add(teams.get(0) + teams.get(1));
                matches.add(teams.get(2) + teams.get(3));
                break;
            case 2:
                matches.add(teams.get(0) + teams.get(3));
                matches.add(teams.get(1) + teams.get(2));
                break;
            case 3:
                matches.add(teams.get(0) + teams.get(2));
                matches.add(teams.get(1) + teams.get(3));
                break;
        }
        return matches;
    }
    @Override
    public int compare(Object team1, Object team2) {
        if (team1.equals(team2)) // really dont know why????
            return 1;
        // So sanh diem trong bang
        int score1 = mapScoresToSort.get(team1);
        int score2 = mapScoresToSort.get(team2);
        if (score1 > score2) return -1;
        if (score1 < score2) return 1;
        
        if (score1 == score2) // neu hoa thi so sanh yff doi dau
        {
            int weekmeet = 0;
            try {
                // xac dinh vong dau ma 2 doi gap nhau de so sanh diem yff
                weekmeet = getWeekThatMeet((String)team1, (String)team2);
            } catch (Exception ex) {
                Logger.getLogger(CupScoreComparator.class.getName()).log(Level.SEVERE, null, ex);
            }
            // lay diem yff                    
            float yffscore1 = Float.valueOf(myUtil.getWeekYffPoint((String)team1, weekmeet));
            float yffscore2 = Float.valueOf(myUtil.getWeekYffPoint((String)team2, weekmeet));
            
            if (yffscore1 > yffscore2) return -1;
            if (yffscore1 < yffscore2) return 1;
            
            if (yffscore1 == yffscore2)     // hoa hieu so doi dau so sanh hieu so toan bang
            {
                float diff1 = mapDiff.get(team1);
                float diff2 = mapDiff.get(team2);
                if (diff1 > diff2) return -1;
                if (diff1 < diff2) return 1;
                
                if (diff1 == diff2) // hoa hieu so toan bang so sanh tong diem yff
                {
                     
                    String totalPoint1 = (String)myUtil.teamsscores.get(team1);
                    int pos = totalPoint1.lastIndexOf("|");
                    float p1 = Float.valueOf(totalPoint1.substring(pos + 1, totalPoint1.length()));
                    
                    String totalPoint2 = (String)myUtil.teamsscores.get(team2);
                    pos = totalPoint2.lastIndexOf("|");
                    float p2 = Float.valueOf(totalPoint2.substring(pos + 1, totalPoint2.length()));
                    
                    if (p1 > p2) return -1;
                    if (p1 < p2) return 1;
                    
                }
            }
            
            
        }
        return 1;
    }
    
}
